package com.fzkj.socket;

import java.io.*;
import java.net.Socket;

public class Client {

    public static void main(String[] args) {
        // 创建请求的socjet
        Socket s = null;
        BufferedWriter writer = null;
        BufferedReader reader = null; // 从控制台读取数据
        try {
            s = new Socket("127.0.0.1",1122);
            System.out.println("建立连接。。。。。");

            // 范围   41-5a，61-7a
            // 手环id 1 ：0x41,0x49,0x62,0x4d,0x61,0x5a,0x4a,0x6b,0x7a,0x5a,0x6f,0x4c
            // 手环id 2 ：0x44,0x41,0x55,0x4f,0x68,0x6a,0x47,0x47,0x77,0x66,0x67,0x71
            // 手环id 3 ：0x45,0x48,0x51,0x4f,0x61,0x7a,0x57,0x53,0x79,0x56,0x71,0x74
            // 手环id 4 ：0x47,0x45,0x57,0x56,0x66,0x6c,0x67,0x77,0x76,0x52,0x79,0x7a
            // 手环id 5 : 0x41,0x49,0x52,0x4d,0x61,0x5a,0x4a,0x6b,0x7a,0x5a,0x6f,0x4c

//             赛车环节开始
            byte[] buffer = {0x0a,0x0a,0x03,0x0b,0x0b};
//             赛车加分
//            byte[] buffer = {0x0a,0x0a,0x01,0x07,0x0d,0x0b,0x0b};
//             赛车减分
//            byte[] buffer = {0x0a,0x0a,0x02,0x08,0x0c,0x0b,0x0b};
//             占点
//            byte[] buffer = {0x0a,0x0a,0x04,0x41,0x49,0x52,0x4d,0x61,0x5a,0x4a,0x6b,0x7a,0x5a,0x6f,0x4c,0x0d,0x0b,0x0b};
//              激活
//            byte[] buffer = {0x0a,0x0a,0x05,0x41,0x49,0x52,0x4d,0x61,0x5a,0x4a,0x6b,0x7a,0x5a,0x6f,0x4c,0x0b,0x0b};

//            DataOutputStream dos = new DataOutputStream(s.getOutputStream());
//            dos.write(buffer);
//            dos.flush();

            while (true){
                InputStream in = s.getInputStream();
                byte[] buf = new byte[24];
                System.out.println("等待数据======");
                int len = in.read(buf);
                for (byte b:buf
                ) {
                    System.out.print(b + " ");
                }
            }

        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                if (writer != null){
                    writer.close();
                }
                if (s != null){
                    s.close();
                }
                System.out.println("关闭连接");
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

    }

    /**
     * 将十六进制的字符串转换成字节数组
     *
     * @param hexString
     * @return
     */
    public static byte[] hexStrToByteArrs(String hexString) {
        if ("".equals(hexString)) {
            return null;
        }

        hexString = hexString.replaceAll(" ", "");
        int len = hexString.length();
        int index = 0;

        byte[] bytes = new byte[len / 2];

        while (index < len) {
            String sub = hexString.substring(index, index + 2);
            bytes[index / 2] = (byte) Integer.parseInt(sub, 16);
            index += 2;
        }

        return bytes;
    }

    /**
     * 数组转换成十六进制字符串
     *
     * @return HexString
     */
    public static final String bytesToHexString(byte[] bArray) {
        StringBuffer sb = new StringBuffer(bArray.length);
        String sTemp;
        for (int i = 0; i < bArray.length; i++) {
            sTemp = Integer.toHexString(0xFF & bArray[i]);
            if (sTemp.length() < 2)
                sb.append(0);
            sb.append(sTemp.toUpperCase());
            // 在这里故意追加一个逗号便于最后的区分
            sb.append(" ");
        }
        return sb.toString();
    }
}
